#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QSound>

chooselevelScene::chooselevelScene(QWidget *parent) : QMainWindow(parent)
{
    //设置窗口固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("选择关卡");

    //创建菜单栏
    QMenuBar * bar = this->menuBar();
    this->setMenuBar(bar);
    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");
    //创建按钮菜单项
    QAction *quitAction = startMenu->addAction("退出");

    connect(quitAction,&QAction::triggered,[=](){this->close();});

    //选择关卡按钮音效
    QSound *chooseSound = new QSound(":/res/TapButtonSound.wav",this);
    //返回按钮音效
    QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);

    //返回按钮
    MyPushbutton * closeBtn = new MyPushbutton(":/res/BackButton.png",":/res/BackButtonSelected.png");

    closeBtn->setParent(this);
    closeBtn->move(this->width() - closeBtn->width(),this->height() - closeBtn->height());

    connect(closeBtn,&MyPushbutton::clicked,[=](){
        //qDebug()<<"点击了返回按钮";
        QTimer::singleShot(500, this,[=](){
            this->hide();
            backSound->play();
            //触发自定义信号，关闭自身，该信号写到 signals下做声明
            emit this->chooseSceneBack();
        });
    });


    //创建选择关卡的按钮
    for(int i = 0 ; i < 20;i++){
         MyPushbutton * menuBtn = new MyPushbutton(":/res/LevelIcon.png");
         menuBtn->setParent(this);
         menuBtn->move(25 + (i%4)*70 , 130+ (i/4)*70);

         //监听选择关卡按钮的信号槽
         connect(menuBtn,&MyPushbutton::clicked,[=](){

             chooseSound->play();
             // qDebug() << "select: " << i;
             if(playscene == NULL)  //游戏场景最好不用复用，直接移除掉创建新的场景
                {
                    this->hide();
                    playscene = new playScene(i+1); //将选择的关卡号 传入给PlayerScene
                    playscene->setGeometry(this->geometry());
                    playscene->show();
                 }


                   connect(playscene,&playScene::chooseSceneBack,[=](){
                           this->setGeometry(playscene->geometry());
                           this->show();
                           delete playscene;
                           playscene = NULL;
                     });
             });


         //按钮上显示的文字
         QLabel * label = new QLabel;
         label->setParent(this);
         label->setFixedSize(menuBtn->width(),menuBtn->height());
         label->setText(QString::number(i+1));

         label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //设置居中
         label->move(25 + (i%4)*70 , 130+ (i/4)*70);
         label->setAttribute(Qt::WA_TransparentForMouseEvents,true);  //鼠标事件穿透
    }




}

void chooselevelScene::paintEvent(QPaintEvent *){
    QPainter qpainter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    qpainter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题
    pix.load(":/res/Title.png");
    qpainter.drawPixmap( (this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);


}


